﻿namespace Dcms.Common
{
    using System;
    using System.IO;
    using System.Runtime.InteropServices;
    using System.Text;

    public class DcmsFileManagerProcessor
    {
        public static int CreateFolder(string FolderPath)
        {
            DirectoryInfo Dir = new DirectoryInfo(FolderPath);
            if (Dir.Exists) return 1;
            try
            {
                Dir.Create();
                return 0;
            }
            catch
            {
                return 2;
            }
        }

        public static int DeleteFileFolder(string FilePath)
        {
            if (File.Exists(FilePath))
            {
                try
                {
                    File.Delete(FilePath);
                    return 0;
                }
                catch
                {
                    return 1;
                }
            }
            DirectoryInfo MyFolder = new DirectoryInfo(FilePath);
            if (MyFolder.Exists)
            {
                if (MyFolder.GetFileSystemInfos().Length == 0)
                {
                    Directory.Delete(FilePath, true);
                    return 0;
                }
                return 1;
            }
            return 1;
        }

        public static int DeleteFileFolder(string FilePath, string DelType)
        {
            if (DelType == "file")
            {
                if (File.Exists(FilePath))
                {
                    try
                    {
                        File.Delete(FilePath);
                        return 0;
                    }
                    catch
                    {
                        return 1;
                    }
                }
                return 0;
            }
            if (Directory.Exists(FilePath))
            {
                try
                {
                    Directory.Delete(FilePath, true);
                    return 0;
                }
                catch
                {
                    return 1;
                }
            }
            return 0;
        }

        private static Encoding GetEncoding(FileStream p_fs, out string p_fileEncode)
        {
            Encoding targetEncoding = Encoding.Default;
            p_fileEncode = "ANSI";
            if (p_fs != null && p_fs.Length >= 1)
            {
                string byte1 = "";
                string byte2 = "";
                string byte3 = "";
                p_fs.Seek(0, SeekOrigin.Begin);
                byte1 = Convert.ToByte(p_fs.ReadByte()).ToString("X");
                if (p_fs.Length >= 2) byte2 = Convert.ToByte(p_fs.ReadByte()).ToString("X");
                if (p_fs.Length >= 3) byte3 = Convert.ToByte(p_fs.ReadByte()).ToString("X");
                if (byte1 == "EF" && byte2 == "BB" && byte3 == "BF")
                {
                    targetEncoding = Encoding.UTF8;
                    p_fileEncode = "UTF-8";
                }
                else if (byte1 == "FF" && byte2 == "FE")
                {
                    targetEncoding = Encoding.Unicode;
                    p_fileEncode = "Unicode";
                }
                else if (byte1 == "FE" && byte2 == "FF")
                {
                    targetEncoding = Encoding.BigEndianUnicode;
                    p_fileEncode = "Unicode big endian";
                }
                p_fs.Seek(0, SeekOrigin.Begin);
            }
            return targetEncoding;
        }

        public static string ReadTextFile(string FilePath)
        {
            if (File.Exists(FilePath))
            {
                StreamReader objReader = new StreamReader(FilePath, Encoding.Default);
                try
                {
                    return objReader.ReadToEnd();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    objReader.Close();
                    objReader.Dispose();
                }
            }
            return "";
        }

        public static string ReadTextFile(string FilePath, string CodeType)
        {
            StreamReader objReader;
            if (!File.Exists(FilePath)) goto Label_00B8;
            Encoding MyEncodInfo = Encoding.UTF8;
            string csTemp0002 = CodeType.ToLower();
            if (csTemp0002 != null)
            {
                if (!(csTemp0002 == "utf-8"))
                {
                    if (csTemp0002 == "unicode")
                    {
                        MyEncodInfo = Encoding.Unicode;
                        goto Label_008E;
                    }
                    if (csTemp0002 == "utf-32")
                    {
                        MyEncodInfo = Encoding.UTF32;
                        goto Label_008E;
                    }
                    if (csTemp0002 == "utf-7")
                    {
                        MyEncodInfo = Encoding.UTF7;
                        goto Label_008E;
                    }
                }
                else
                {
                    MyEncodInfo = Encoding.UTF8;
                    goto Label_008E;
                }
            }
            MyEncodInfo = Encoding.UTF8;
        Label_008E:
            objReader = new StreamReader(FilePath, MyEncodInfo);
            try
            {
                return objReader.ReadToEnd();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                objReader.Close();
                objReader.Dispose();
            }
        Label_00B8:
            return "";
        }

        public static string ReadTextFile(string FilePath, out string FileEncode)
        {
            if (File.Exists(FilePath))
            {
                string FileContent = "";
                FileStream fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read);
                StreamReader sr = new StreamReader(fs, GetEncoding(fs, out FileEncode));
                FileContent = sr.ReadToEnd();
                sr.Close();
                fs.Close();
                return FileContent;
            }
            FileEncode = "utf-8";
            return "";
        }

        public static int RenameFileFolder(string OldFileName, string NewFileName, string TypeStr)
        {
            if (TypeStr == "file")
            {
                if (File.Exists(OldFileName))
                {
                    try
                    {
                        File.Move(OldFileName, NewFileName);
                        return 0;
                    }
                    catch
                    {
                        return 1;
                    }
                }
                return 0;
            }
            if (Directory.Exists(OldFileName))
            {
                try
                {
                    Directory.Move(OldFileName, NewFileName);
                    return 0;
                }
                catch
                {
                    return 1;
                }
            }
            return 0;
        }

        public static int SaveTextFile(string FilePath, string FileContent, string FileEncode)
        {
            int result = 0;
            Encoding setEncoding = Encoding.Default;
            if (FileEncode.ToLower() == "utf-8")
                setEncoding = Encoding.UTF8;
            else if (FileEncode.ToLower() == "unicode")
                setEncoding = Encoding.Unicode;
            else if (FileEncode.ToLower() == "unicode big endian") setEncoding = Encoding.BigEndianUnicode;
            FileStream fs = new FileStream(FilePath, FileMode.Create, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs, setEncoding);
            try
            {
                sw.Write(FileContent);
                sw.Flush();
                result = 0;
            }
            catch (Exception)
            {
                result = 1;
            }
            finally
            {
                sw.Close();
                fs.Close();
            }
            return result;
        }
    }
}

